package com.zjxf.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhan
 * @date 2020/04/08 17:19:14
 */
public class AddressUtils {

    /**
     * 解析地址
     *
     * @param address 本地地址
     * @return Map<String, String>
     */
    public static Map<String, String> getAddressInfo(String address) {
        String province = null, city = null, provinceAndCity, town = null;
        Map<String, String> row = new LinkedHashMap<>();
        List<Map<String, String>> table = new ArrayList<>();
        Map<String, String> resultMap = new HashMap<>(4);

        if (address.startsWith("香港特别行政区")) {
            resultMap.put("province", "香港");
            return resultMap;
        } else if (address.contains("澳门特别行政区")) {
            resultMap.put("province", "澳门");
            return resultMap;
        } else if (address.contains("台湾")) {
            resultMap.put("province", "台湾");
            return resultMap;
        } else {
            String regex = "((?<provinceAndCity>[^市]+市|.*?自治州|.*?区|.*县)(?<town>[^区]+区|.*?市|.*?县|.*?路|.*?街|.*?道|.*?镇|.*?旗)(?<detailAddress>.*))";
            Matcher m = Pattern.compile(regex).matcher(address);
            while (m.find()) {
                provinceAndCity = m.group("provinceAndCity");
                String regex2 = "((?<province>[^省]+省|.+自治区|上海市|北京市|天津市|重庆市|上海|北京|天津|重庆)(?<city>.*))";
                Matcher m2 = Pattern.compile(regex2).matcher(provinceAndCity);
                while (m2.find()) {
                    province = m2.group("province");
                    row.put("province", province == null ? "" : province.trim());
                    city = m2.group("city");
                    row.put("city", city == null ? "" : city.trim());
                }
                town = m.group("town");
                row.put("town", town == null ? "" : town.trim());
                table.add(row);
            }
        }
        if (table.size() > 0) {
            if (StringUtils.isNotBlank(table.get(0).get("province"))) {
                province = table.get(0).get("province");
                //对自治区进行处理
                if (province.contains("自治区")) {
                    if (province.contains("内蒙古")) {
                        province = province.substring(0, 4);
                    } else {
                        province = province.substring(0, 3);
                    }

                }
            }
            if (StringUtils.isNotBlank(province)) {
                if (StringUtils.isNotBlank(table.get(0).get("city"))) {
                    city = table.get(0).get("city");
                    if (city.equals("上海市") || city.equals("重庆市") || city.equals("北京市") || city.equals("天津市")) {
                        province = table.get(0).get("city");
                    }
                } else if (province.equals("上海市") || province.equals("重庆市") || province.equals("北京市") || province.equals("天津市")) {
                    city = province;
                }
                if (StringUtils.isNotBlank(table.get(0).get("town"))) {
                    town = table.get(0).get("town");
                }
                province = province.substring(0, province.length() - 1);

            }

        } else {
            return resultMap;
        }
        resultMap.put("province", province);
        resultMap.put("city", city);
        resultMap.put("district", town);

        return resultMap;
    }

    /**
     * 校验地址是否合法
     *
     * @param locationStr 地址字符串
     * @return Boolean
     */
    public static Boolean checkLocationStr(String locationStr) {
        String orDefault = getAddressInfo(locationStr).getOrDefault("district", StringUtils.EMPTY);
        return !orDefault.equals(StringUtils.EMPTY);
    }

    public static void main(String[] args) {
        System.out.println(checkLocationStr("山西省太原市万柏林区G5京昆高速2号靠近裕峰花园"));
    }
}
